package com.ruoyi.group.service.impl;

import com.ruoyi.group.Utils.MyUtil;
import com.ruoyi.group.domain.Orderdetail;
import com.ruoyi.group.domain.RpRepairOrder;
import com.ruoyi.group.domain.dto.RepairListDto;
import com.ruoyi.group.domain.dto.RepairOrderDto;
import com.ruoyi.group.domain.dto.RepairOrderQualitiedDto;
import com.ruoyi.group.domain.dto.daomin.RepairOrderDtoMin;
import com.ruoyi.group.mapper.RepairOrderMapper;
import com.ruoyi.group.service.IRepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:黄志超
 * @Todo:订单插入操作
 * @DateTime:2022/3/8 14:55
 * @Component:蜗牛
 **/
@Service
public class IRepairOrderServiceImpl implements IRepairOrderService {
    @Autowired
    RepairOrderMapper repairOrderMapper;
    @Override
    public boolean OrderJoin(RpRepairOrder repairOrder) {

        String DATA = new MyUtil().GenerateOrder();
        //获取订单号
        repairOrder.setRepairNo(DATA);

        //转换类型是否匿名 1代表是匿名 0代表不是匿名
//        if(repairOrder.isIsniming()){
//            repairOrder.setIsAnonymous(1);
//        }else{
//            repairOrder.setIsAnonymous(1);
//        }
//        SimpleDateFormat dmDate = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
//
//       //创建时间
//        repairOrder.setCreateTime(new MyUtil().GetDate());

        repairOrder.setIsDelete(0);
        int i = repairOrderMapper.OrderJoin(repairOrder);
        return i>0;
    }

    //根据用户ID查询自己对应的工单详情表
    @Override
    public List<RepairOrderDto> findOrder(RepairOrderDtoMin repairOrderDtoMin) {

        return repairOrderMapper.findOrder(repairOrderDtoMin);
    }

    //根据工单编号查询工单详情
    @Override
    public Orderdetail findSerialNumber(String serialnumber) {
        return repairOrderMapper.findSerialNumber(serialnumber);
    }

    @Override
    public boolean delectOrder(String orderOn) {
        int i = repairOrderMapper.delectOrder(orderOn);
        return i>0;
    }

    @Override
    public List<RepairOrderQualitiedDto> findQualitied(RpRepairOrder rpRepairOrder) {
        List<RepairOrderQualitiedDto> dtos = repairOrderMapper.findQualitiedOrder(rpRepairOrder);
        for (RepairOrderQualitiedDto repairOrderQualitiedDto:dtos) {
            if (repairOrderQualitiedDto.getRepairDept()!=null) {
                if(repairOrderQualitiedDto.getAttitudeCore()!=null){
                    int attitudeCore =Integer.parseInt(repairOrderQualitiedDto.getAttitudeCore());
                    int languageCore =Integer.parseInt(repairOrderQualitiedDto.getLanguageCore());
                    int timelyCore =Integer.parseInt(repairOrderQualitiedDto.getTimelyCore());
                    int totalCore=attitudeCore+languageCore+timelyCore;
                    repairOrderQualitiedDto.setTotalCore((totalCore+""));
                }
                //工单所属部门与发起人的集合
                repairOrderQualitiedDto.setRepairByAndDept(repairOrderQualitiedDto.getRepairDept()+"/"+repairOrderQualitiedDto.getRepairBy());
            }
        }
        return dtos;
    }

    @Override
    public RepairOrderDto findRepairById(int id) {
        return repairOrderMapper.findRepairById(id);
    }

    @Override
    public List<RepairListDto> findOrderList(RpRepairOrder rpRepairOrder) {
        List<RepairListDto> orderList = repairOrderMapper.findOrderList(rpRepairOrder);
        for (RepairListDto repairListDto:orderList) {
            if (repairListDto.getRepairBy()!=null) {
                //工单所属部门与发起人的集合
                repairListDto.setRepairByAndDept(repairListDto.getRepairDept()+"/"+repairListDto.getRepairBy());
            }
        }
        return orderList;
    }
}
